package com.vint.mi;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 1. done,easy
 * 2. BFS
 * 马走日
 */
public class Mi56 {

    public static String solution(String line){
        String[] src = line.split(";");
        int m = Integer.parseInt(src[0]);
        int n = Integer.parseInt(src[1]);
        int x1 = Integer.parseInt(src[2]);
        int y1 = Integer.parseInt(src[3]);
        int x2 = Integer.parseInt(src[4]);
        int y2 = Integer.parseInt(src[5]);
        boolean[][] tag = new boolean[m+1][n+1];
        for(int i = 0; i <=m; i++){
            for(int j = 0; j <=n; j++) {
                tag[i][j] = false;
            }
        }
        return minLeap(tag,m,n,x1,y1,x2,y2)+"";
    }

    public static int minLeap(boolean[][]tag,int m, int n, int x1, int y1, int x2, int y2){
        int leap = 0;
	    int leavel = 0;
        Queue<Point> queue = new LinkedList<>();
        Point point1 = new Point(x1,y1);
        queue.add(point1);
        tag[point1.x][point1.y] = true;
        leavel = 1;
        while(!queue.isEmpty()){
        	int l = leavel;
        	leavel = 0;
        	for(int i=0;i<l;i++) {
                Point point =  queue.poll();
                if (point.x - 1 >= 0 && point.y - 2 >= 0 && !tag[point.x - 1][point.y - 2]) {
                	if(point.x-1==x2&&point.y-2==y2){
                	    return ++leap;
                    }
                    queue.add(new Point(point.x - 1, point.y - 2));
                    tag[point.x - 1][point.y - 2] = true;
                    leavel++;
                }
                if (point.x - 1 >= 0 && point.y + 2 <= n && !tag[point.x - 1][point.y + 2]){
                    if(point.x-1==x2&&point.y+2==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x - 1, point.y + 2));
                    tag[point.x - 1][point.y + 2] = true;
                    leavel++;
                }
                if (point.x + 1 <= m && point.y + 2 <= n && !tag[point.x + 1][point.y + 2]){
                    if(point.x+1==x2&&point.y+2==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x + 1, point.y + 2));
                    tag[point.x + 1][point.y + 2] = true;
                    leavel++;
                }
                if (point.x + 1 <= m && point.y - 2 >= 0 && !tag[point.x + 1][point.y - 2]){
                    if(point.x+1==x2&&point.y-2==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x + 1, point.y - 2));
                    tag[point.x + 1][point.y - 2] = true;
                    leavel++;
                }
                if (point.x - 2 >= 0 && point.y - 1 >= 0 && !tag[point.x - 2][point.y - 1]) {
                    if(point.x-2==x2&&point.y-1==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x - 2, point.y - 1));
                    tag[point.x - 2][point.y - 1] = true;
                    leavel++;
                }
                if (point.x - 2 >= 0 && point.y + 1 <= n && !tag[point.x - 2][point.y + 1]) {
                    if(point.x-2==x2&&point.y+1==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x - 2, point.y + 1));
                    tag[point.x - 2][point.y + 1] = true;
                    leavel++;
                }
                if (point.x + 2 <= m && point.y + 1 <= n && !tag[point.x + 1][point.y + 1]) {
                    if(point.x+2==x2&&point.y+1==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x + 2, point.y + 1));
                    tag[point.x + 2][point.y + 1] = true;
                    leavel++;
                }
                if (point.x + 2 <= m && point.y - 1 >= 0 && !tag[point.x + 1][point.y - 1]) {
                    if(point.x+2==x2&&point.y-1==y2){
                        return ++leap;
                    }
                    queue.add(new Point(point.x + 2, point.y - 1));
                    tag[point.x + 2][point.y - 1] = true;
                    leavel++;
                }
            }
            ++leap;
        }
        return -1;
    }

    static class Point{
        int x;
        int y;
        public Point(int xx, int yy){
            x = xx;
            y = yy;
        }
    }
    public static void main(String[] args) {
        System.out.println(solution("1;1;0;0;1;1"));
        System.out.println(solution("100;200;0;0;10;100"));
        System.out.println(solution("2;2;0;0;2;2"));
    }
}
